package qgs.ddjdispachingsystem.ddj.wcs.taskPool;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qgs.ddjdispachingsystem.ddj.ioserver.IOServerService;
import qgs.ddjdispachingsystem.ddj.wcs.entity.CargoArg;
import qgs.ddjdispachingsystem.ddj.wcs.enums.IOServerEnum;

import java.util.concurrent.Callable;

/**
 * @Author: 蔡翔
 * @Date: 2019/10/30 16:23
 * @Version 1.0
 */
public class DDJGetCargoArgRunnable<T> implements Callable<T> {
    private final static Logger logger = LoggerFactory.getLogger(DDJGetCargoArgRunnable.class);
    private IOServerService ioServerService;
    public DDJGetCargoArgRunnable(IOServerService ioServerService){
        this.ioServerService = ioServerService;
    }
    public DDJGetCargoArgRunnable(){

    }
    public synchronized void initialDDJGetCargoArgRunnable(IOServerService ioServerService){
        this.ioServerService = ioServerService;
    }
    private CargoArg cargoArg = new CargoArg();
    //todo  后期要修改 数据类型（默认为 0 就是）
    private Short cargoWeight = 0;
    private Short cargoHeight = 0;

    @Override
    public T call() throws Exception {
        while (true){
            try {
                // 要改plc 点位
                cargoWeight = ioServerService.getValueByName(IOServerEnum.Cargo_Weight.getCollectionPointName()).i2Val;
                cargoHeight = ioServerService.getValueByName(IOServerEnum.Cargo_Height.getCollectionPointName()).i2Val;
            }catch (Exception e){
                logger.info("监听货物重量 高度异常,异常是："+e.toString());
            }
            if(cargoHeight != 0 && cargoWeight != 0){
                cargoArg = new CargoArg();
                cargoArg.setCargoHeight(cargoHeight);
                cargoArg.setCargoWeight(cargoWeight);
                break;
            }else {
                cargoArg = null;
            }
            Thread.sleep(500);
        }
        return (T)cargoArg;
    }
}
